<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use api\user\model\UserBalanceLogModel;
use api\user\model\UserModel;
use app\portal\model\PortalCategoryPostModel;
use app\portal\model\PortalTagPostModel;
use app\portal\model\QuestionsModel;
use app\portal\model\RecycleBinModel;
use cmf\controller\AdminBaseController;
use app\portal\model\PortalPostModel;
use app\portal\service\PostService;
use app\portal\model\PortalCategoryModel;
use app\admin\model\ThemeModel;
use think\db\Query;

class AdminQuestionsController extends AdminBaseController
{
    /**
     * 文章列表
     * @adminMenu(
     *     'name'   => '文章管理',
     *     'parent' => 'portal/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文章列表',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $keyword = $this->request->param('keyword', '');
        $status = $this->request->param('status', '');

        $questionsModel = new QuestionsModel();
        $list = $questionsModel::where(
            function (Query $query) {
                $data = $this->request->param();

                $keyword = $this->request->param('keyword', '');
                $status = $this->request->param('status', '');

                if (!empty($status)) {
                    $query->where('status', intval($status));
                }else{
                    $query->where('status', '<>',0);
                }
                $admin_id = cmf_get_current_admin_id();
                if ($admin_id != 1){
                    $query->where('q_id', '=',$admin_id);
                }

                if (!empty($data['keyword'])) {
                    $query->where('description|answer', 'like', "%$keyword%");
                }

            }
        )->order('create_time desc')->paginate(10);

        $userModel = new UserModel();
        foreach ($list as &$item){
            if ($item['admin_id']){
                $userINfo = $userModel->find($item['admin_id']);
                $item['admin_name'] = $userINfo['user_login'];
            }
            if ($item['user_id']){
                $userINfo = $userModel->find($item['user_id']);
                $item['mobile'] = $userINfo['mobile'];
            }

        }


        $this->assign('keyword', $keyword);
        $this->assign('status', $status);
        $this->assign('list', $list->items());
        $this->assign('page', $list->render());


        return $this->fetch();
    }



    /**
     * 编辑文章
     * @adminMenu(
     *     'name'   => '编辑文章',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑文章',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {

        $id = $this->request->param('id', 0, 'intval');

        $questionModel = new QuestionsModel();
        $post            = $questionModel->where('id', $id)->find();

        $this->assign('post', $post);

        return $this->fetch();
    }

    /**
     * 编辑文章提交
     * @adminMenu(
     *     'name'   => '编辑文章提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑文章提交',
     *     'param'  => ''
     * )
     * @throws \think\Exception
     */
    public function editPost()
    {

        if ($this->request->isPost()) {
            $data = $this->request->param();

            $post   = $data['post'];

            $update = [];
            //$update['description'] = $post['description'];
            $update['answer'] = $post['answer'];
            $update['admin_id'] = cmf_get_current_admin_id();

            $portalPostModel = new QuestionsModel();
            $portalPostModel->where('id',$post['id'])->update($update);

            $this->success('保存成功!');
        }
    }

    public function delete()
    {
        $param           = $this->request->param();

        if (isset($param['id'])) {
            $id           = $this->request->param('id', 0, 'intval');
            QuestionsModel::where('id', $id)->update(['status' => 0]);

            $this->success("删除成功！", '');
        }

    }

    /**
     * 文章发布
     * @adminMenu(
     *     'name'   => '文章发布',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文章发布',
     *     'param'  => ''
     * )
     */
    public function upStatus()
    {
        $portalPostModel = new QuestionsModel();

        $id = $this->request->param('id');
        $status = $this->request->param('status');


        $portalPostModel->where('id', '=', $id)->update(['status' => $status]);
        $this->success("操作成功！", '');

    }



}
